/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelAgenda.java
 *
 * Created on 26-oct-2011, 14:38:08
 */
package co.edu.eam.clinica.vista.gui.consulta;

import co.edu.eam.clinica.vista.controladores.consulta.controladorConsultaClinica;
import co.edu.eam.clinica.vista.controladores.paciente.controladorPaciente;
import co.edu.eam.clinica.vista.gui.common.PanelOncologos;
import co.edu.eam.clinica.vista.gui.common.VentanaPrincipalManager;
import co.edu.eam.persitencia.entidades.Consulta;
import co.edu.eam.persitencia.entidades.Paciente;
import co.edu.eam.persitencia.entidades.Procedimiento;
import co.edu.eam.persitencia.enumeraciones.TipoDocumentoEnum;
import co.edu.eam.persitencia.enumeraciones.TipoProcedimietoEnum;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Camilo Andres
 */
public class PanelBuscarCitas extends PanelOncologos {

    /**
     * Creates new form PanelAgenda
     */
    public controladorConsultaClinica controlConsulta;
    public controladorPaciente controlPaciente;
    
    public PanelBuscarCitas() {
        
        initComponents();
        setSize(500, 500);
        setVisible(true);
        controlConsulta = new controladorConsultaClinica();
        controlPaciente = new controladorPaciente(null);
        llenarCProcedimiento();
        llenarCTipoDocumento();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaCitas = new javax.swing.JTable();
        bIrCita = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        bBuscar = new javax.swing.JButton();
        tfPaciente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfNombrePac = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfEPS = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cbTipoDoc = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cbTipoProc = new javax.swing.JComboBox();
        bVerCita = new javax.swing.JButton();

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Citas"));

        tablaCitas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "id", "Procedimiento", "Fecha/Hora"
            }
        ));
        jScrollPane1.setViewportView(tablaCitas);
        tablaCitas.getColumnModel().getColumn(1).setResizable(false);
        tablaCitas.getColumnModel().getColumn(2).setResizable(false);

        bIrCita.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/guion-formato-mas-icono-7247-32 - copia.png"))); // NOI18N
        bIrCita.setText("Ir");
        bIrCita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bIrCitaActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Paciente");

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/buscar-buscar-ampliar-icono-4271-32.png"))); // NOI18N
        bBuscar.setText("Buscar");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Nombre");

        tfNombrePac.setEditable(false);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("EPS");

        tfEPS.setEditable(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Tipo doc");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfEPS, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tfPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbTipoDoc, 0, 198, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bBuscar))
                    .addComponent(tfNombrePac, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE))
                .addGap(250, 250, 250))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)
                        .addComponent(cbTipoDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bBuscar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfNombrePac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfEPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel4.setText("Procedimiento");

        cbTipoProc.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipoProcItemStateChanged(evt);
            }
        });
        cbTipoProc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTipoProcActionPerformed(evt);
            }
        });

        bVerCita.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/impresora-icono-7004-32.png"))); // NOI18N
        bVerCita.setText("Ver");
        bVerCita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bVerCitaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(bIrCita)
                                .addGap(42, 42, 42)
                                .addComponent(bVerCita))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(cbTipoProc, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 592, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cbTipoProc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bIrCita)
                    .addComponent(bVerCita))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        if (tfPaciente.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "digite el documento");
            tfPaciente.requestFocus();
        }else{
             String documento = tfPaciente.getText();
        
        try {
            Paciente paciente = controlConsulta.buscarPaciente(documento);
            
            tfNombrePac.setText(paciente.getNombre());
            tfEPS.setText(paciente.getEps().getNombre());
            cbTipoDoc.setSelectedItem(paciente.getTipoDocumento());
            
            //llenar la tabla
            
            DefaultTableModel modelo = new DefaultTableModel();
            this.tablaCitas.setModel(modelo);
            
            modelo.setColumnCount(0);
            modelo.addColumn("id");
            modelo.addColumn("procedimiento");
            modelo.addColumn("fecha/hora");
            
            List lista = controlConsulta.listarConsulta_paciente(paciente.getDocumento());
            modelo.setNumRows(lista.size());
            
            for (int i = 0; i < lista.size(); i++) {
                
                Consulta con = (Consulta) lista.get(i);
                
                modelo.setValueAt(con.getId(), i, 0);
                modelo.setValueAt(con.getProcedimiento().getDescripcion(), i, 1);
                modelo.setValueAt(con.getFechaHora(), i, 2);
                
                this.tablaCitas.setModel(modelo);
            }
            
        } catch (Exception ex) {
            Logger.getLogger(PanelBuscarCitas.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_bBuscarActionPerformed
    
    private void bIrCitaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bIrCitaActionPerformed
        //POR PARAMETRO DEL PANEL EL ID DE LA CITA
        try {
            DefaultTableModel modelo=(DefaultTableModel) tablaCitas.getModel();
            int id= tablaCitas.getSelectedRow();
            long idconsulta=(long) tablaCitas.getValueAt(id, 0);
            
            PanelCitaPac panel = new PanelCitaPac(idconsulta);
            VentanaPrincipalManager.agregarVentana(panel);
//            
//            this.tablaCitas.setModel(modelo);
        } catch (Exception ex) {
            Logger.getLogger(PanelBuscarCitas.class.getName()).log(Level.SEVERE, null, ex);
        }
            
         
        
    }//GEN-LAST:event_bIrCitaActionPerformed
    
    private void cbTipoProcItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipoProcItemStateChanged
        TipoProcedimietoEnum pro =  (TipoProcedimietoEnum) cbTipoProc.getSelectedItem();
        
        try {
            DefaultTableModel modelo = new DefaultTableModel();
            this.tablaCitas.setModel(modelo);
 
            modelo.setColumnCount(0);
            modelo.addColumn("id");
            modelo.addColumn("procedimiento");
            modelo.addColumn("fecha/hora");
            
            List lista = controlConsulta.listarProcedimientos_consulta(pro);
            modelo.setNumRows(lista.size());
            
            for (int i = 0; i < lista.size(); i++) {
                
                Consulta con = (Consulta) lista.get(i);
                
                modelo.setValueAt(con.getId(), i, 0);
                modelo.setValueAt(con.getProcedimiento(), i, 1);
                modelo.setValueAt(con.getFechaHora(), i, 2);
                
                this.tablaCitas.setModel(modelo);
            }
        } catch (Exception ex) {
            Logger.getLogger(PanelBuscarCitas.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_cbTipoProcItemStateChanged
    
    private void bVerCitaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bVerCitaActionPerformed
// preguntar que hace el boton reporte 
     
        
        //
    }//GEN-LAST:event_bVerCitaActionPerformed

    private void cbTipoProcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTipoProcActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbTipoProcActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bIrCita;
    private javax.swing.JButton bVerCita;
    private javax.swing.JComboBox cbTipoDoc;
    private javax.swing.JComboBox cbTipoProc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaCitas;
    private javax.swing.JTextField tfEPS;
    private javax.swing.JTextField tfNombrePac;
    private javax.swing.JTextField tfPaciente;
    // End of variables declaration//GEN-END:variables

    private void llenarCProcedimiento() {
           TipoProcedimietoEnum[] pro=TipoProcedimietoEnum.values();
            for (int i = 0; i < pro.length; i++) {
               cbTipoProc.addItem(pro[i]);
            }
    }

    private void llenarCTipoDocumento() {
        TipoDocumentoEnum[] tipo=TipoDocumentoEnum.values();
        for (int i = 0; i < tipo.length; i++) {
            cbTipoDoc.addItem(tipo[i]);
            
        }
    }
}
